<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 20px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>属性列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="属性模版" prop="AttributeTemplateId">
        <el-select v-model="ruleForm.AttributeTemplateId" placeholder="请选择">
          <el-option
              v-for="item in attributeListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" @click="submitForm('ruleForm')">查看</el-button>
      </el-form-item>
    </el-form>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="ID" ></el-table-column>
        <el-table-column prop="name" label="属性名称" ></el-table-column>
        <el-table-column prop="description" label="简介"></el-table-column>
        <el-table-column prop="type" label="类型"></el-table-column>
        <el-table-column prop="unit" label="计量单位"></el-table-column>
        <el-table-column prop="sort" label="排列序号"></el-table-column>
        <el-table-column prop="isAllowCustomize" label="是否允许自定义"></el-table-column>
        <el-table-column  label="按钮">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" circle></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="openDeleteConfirm(scope.row)" circle></el-button>
          </template>
        </el-table-column>
      </el-table>

  </div>
</template>
<script>
export default {
  data() {
    return {
      attributeListOptions: [],
      tableData: [],
      ruleForm: {
        AttributeTemplateId:null
      },
      value:true,
      rules:{}
    }
  },
  methods:{

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/attribute/'+this.ruleForm.AttributeTemplateId+'/select-template';
          console.log('url=' + url);
          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .get(url).then(response => {
            let responseBody = response.data;
            console.log('responseBody=' + responseBody);
            if (responseBody.state == 10000) {
              this.tableData=responseBody.data;
              this.resetForm(formName);

            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleDelete(attribute){
      console.log('准备删除相册.....id='+attribute.id);
      let url = 'http://localhost:9080/attribute/'+attribute.id+'/delete';
      console.log('url ='+url);
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .post(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state!=10000){
          this.$message.error(responseBody.message);
        }else {
          this.$message.success('删除成功');
          window.location.reload()

        }
      })
    },
    openDeleteConfirm(attribute){let message = '此操作将永久删除['+attribute.name+']相册, 是否继续?';
      this.$confirm( message,'提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(attribute);
      }).catch(() => {

      });
    },
    attribuTemplatesList(){
      let url = 'http://localhost:9080/attribute-templates';
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
            let responseBody = response.data;
            this.attributeListOptions=responseBody.data;
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    this.attribuTemplatesList();
  }
}
</script>

